extends Node

var game = null
var logo = null
var title = null
var play = null
var dialog = null
var select = null
var save = null
var library = null
var options = null
var history = null

var play_time = 0
var chapter = '章节信息'

var game_values = {}
var game_common_values = {}

func loadGameJSON(fileName : String):
	var file = File.new()
	var err = file.open("res://game/"+fileName,file.READ)
	if err:
		print('[{name}]打开失败: 错误码 - {code}'.format({
			"name": fileName,
			"code": err
		}))
		return null
	var jsonStr = file.get_as_text()
	file.close()
	var r = JSON.parse(jsonStr)
	if r.error:
		print('[{name}]加载失败: 第{line}行, {msg}'.format({
			"name": fileName,
			"line": r.error_line,
			"msg": r.error_string
		}))
		return null
	return r.result
	
func loadFileAsLine(fileName : String):
	var r = []
	var file = File.new()
	var err = file.open("res://game/"+fileName,file.READ)
	if err:
		print('[{name}]打开失败: 错误码 - {code}'.format({
			"name": fileName,
			"code": err
		}))
		return null
	while !file.eof_reached():
		var text = file.get_line()
		if text.length() == 0:
			continue
		#print(text)
		r.append(text)
	file.close()
	return r

func getSaveDataInfo(number : int):
	var save_pass = Config.save_password
	var save_path = "user://save_data_{number}".format({
		"number": number
	})
	var file_name = save_path + '/' + "savedata.sav"
	var save_game = File.new()
	if not save_game.file_exists(file_name):
		return {}
	var err = save_game.open_encrypted_with_pass(file_name, File.READ, save_pass)
	if err:
		print('loadgame error: ', err)
		return {}
	var line = save_game.get_line()
	save_game.close()
	return parse_json(line)

func getGameValue(key : String, defValue):
	if game_values.has(key):
		return game_values[key]
	game_values[key] = defValue
	return defValue
	
func setGameValue(key : String, value):
	game_values[key] = value
	
func getGameCommonValue(key : String, defValue):
	if game_common_values.has(key):
		return game_common_values[key]
	game_common_values[key] = defValue
	return defValue
	
func setGameCommonValue(key : String, value):
	game_common_values[key] = value
